package yong.birthdayreminder;

import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class AddNewBuddyActivity extends Activity implements OnClickListener{
    private BirthdayReminderDBHelper dbhelper = null;
    private SQLiteDatabase db = null;
    TextView name = null;
    TextView hobby = null;
    TextView birthday = null;
    Button add = null;
    EditText name_v = null;
    EditText hobby_v = null;
    DatePicker birthday_v = null;
    AddNewBuddyActivity thisInstant;
    
    @Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbhelper = new BirthdayReminderDBHelper(getBaseContext());
        db = dbhelper.getWritableDatabase();
        setContentView(R.layout.addbuddy);
        name = (TextView)findViewById(R.id.TextView01);
        hobby = (TextView)findViewById(R.id.TextView02);
        birthday = (TextView)findViewById(R.id.TextView03);
        add = (Button)findViewById(R.id.Button01);
        name.setText(R.string.name);
        hobby.setText(R.string.hobby);
        birthday.setText(R.string.birthday);
        add.setText(R.string.add);
        name_v = (EditText)findViewById(R.id.EditText01);
        hobby_v = (EditText)findViewById(R.id.EditText02);
        birthday_v = (DatePicker)findViewById(R.id.DatePicker01);
        add.setOnClickListener(this);     
        thisInstant = this;
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ContentValues cv = new ContentValues();
		int month = birthday_v.getMonth()+1;
		int day = birthday_v.getDayOfMonth();
		int year = birthday_v.getYear();
		String month_s = null;
		String day_s = null;
		if (month < 10 ) {
			month_s = "0"+month;
		}else {
			month_s = ""+month;
		}
		if (day < 10) {
			day_s = "0" + day;
		}else {
			day_s = "" + day;
		}
			
		//cv.put("_id", 1);
		cv.put("name", name_v.getText().toString());		
        cv.put("birthday", year+"-"+month_s+"-"+day_s);
        cv.put("hobby", hobby_v.getText().toString());
        long id = db.insert(BirthdayReminderDBHelper.BIRTHDAY_TABLE_NAME, null, cv);
               
        //Implement the reminder
        //Start time
        Date d = new Date();
        int thisYear = d.getYear();
        long start = Date.UTC(thisYear, month-1, day, 12, 0, 0);
        if (start < System.currentTimeMillis()) {
        	start = Date.UTC(d.getYear()+1, month-1, day, 12, 0, 0);
        }
        //Show the remind box 3 days before the birthday
        start = start - 3*AlarmManager.INTERVAL_DAY;
        
		AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		Intent intentShowBirthday = new Intent(this, ReminderShowBox.class);
		//Put the Show information in the intent
		intentShowBirthday.putExtra("reminderstring", "   "+name_v.getText().toString() + getResources().getString(R.string.birthdayiscoming));
		intentShowBirthday.putExtra("birthday", month_s+"-"+day_s);
		intentShowBirthday.putExtra("start", start+3*AlarmManager.INTERVAL_DAY);
		//use the id of this reminder as the second parameter: requestCode. This code needs to be sole if you want to implement multiple alarm.
		PendingIntent pendingIntent = PendingIntent.getActivity(this, (int)id, intentShowBirthday, 0);  
		alarmManager.setRepeating(AlarmManager.RTC, start, (long) ((AlarmManager.INTERVAL_DAY)*365.2), pendingIntent);
		//Show the alert dialog
		AlertDialog.Builder added = new AlertDialog.Builder(AddNewBuddyActivity.this);
		added.setTitle(this.getString(R.string.addnewmember));
		added.setMessage(this.getString(R.string.addsuccess));
		added.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
			public void onClick(DialogInterface dialog, int whichButton) {
            		thisInstant.finish();
            }});
		added.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
			public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked Cancel so do some stuff */
            }
        });
		AlertDialog ad = added.create();
		ad.show();
	}
}